package com.ykzb.util;
/***********************************************************************************
 * Copyright (c) 2001-2005 Haichao Info-Tech Co., Ltd.                   *
 * All rights reserved.                                                 *
 * 海潮技术有限公司  版权所有                                            *
 *                                                                     *
 * Created on 2005-8-9                                                  *
 *                                                                     *
 * $Author: zdx<yongli_zbw@163.com>                                      *
 * $Date: 2005/08/09 05:30:26 $                                        *
 * $Revision: 1.1 $                                                     *
 ***********************************************************************************/ 

/**
 * MD5加密类
 */

import java.security.MessageDigest;

public class MD5 {
	
	public static String digest(byte[] src) {
       
		MessageDigest md = null;
		
		try {
			md = MessageDigest.getInstance("md5");
			md.reset();
		} catch(Exception e) {
			//System.out.println("没有md5加密算法");
		}
		
		byte[] tmp = md.digest(src);
		
		return byte2hex(tmp);
	}
	
	/**
	 * 二进制转化为十六进制表示
	 */
	
	private static String byte2hex(byte[] b) {
		
		String hs = "";
		String stmp = "";
		
		for (int n=0;n<b.length;n++) {
			stmp = (Integer.toHexString(b[n] & 0XFF));
			
			if (stmp.length()==1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
			
			//if (n<b.length-1)
				//hs = hs;
		}
		
		return hs.toLowerCase();
	}
}
